package org.neframework.webtest.action.xyk.card;

import java.util.List;
import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_s_customer;
import org.neframework.webtest.model.t_s_customer_card;
import org.neframework.webtest.service.CustomerAnysisService;
import org.neframework.webtest.service.CustomerService;
import org.neframework.webtest.util.date.DateTools;

import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/xyk/card")
public class CardController extends BaseController {

	private static final CustomerService custService = new CustomerService();
	private static final CustomerAnysisService anysisService = new CustomerAnysisService();

	@RequestMapping("/list")
	public String list(String customer_id) {
		t_s_customer customer = (t_s_customer) oc.findObjById(customer_id,
				t_s_customer.class);

		req.setAttribute("customer", customer);
		return "/jsp/xyk/card/list.jsp";
	}

	@RequestMapping("/list_data")
	@ResponseBody
	public void list_data(String customer_id) {

		List<Map<String, Object>> list = custService.selectCustomerCardList(
				customer_id, sort_params, page);

		// 查询流水
		for (Map<String, Object> map : list) {
			String customer_card_id = (String) map.get("kid");
			Map<String, Object> flowMap = anysisService.findFlowMap(
					customer_card_id, DateTools.getToday());
			map.putAll(flowMap);
		}

		result.put("list", list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_s_customer_card card, String card_date,
			String card_end_date) {
		card.setDate(DateTools.strToDate(card_date));
		card.setEnd_date(DateTools.strToDate(card_end_date, "yyyy-MM"));
		oc.saveObj(card);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_s_customer_card card = (t_s_customer_card) oc.findObjById(kid,
				t_s_customer_card.class);

		Map<String, Object> map1 = JsonTools.objToMap("card", card);
		map1.put("card_date", DateTools.parse(card.getDate()));
		map1.put("card_end_date", DateTools.parse(card.getEnd_date()));
		map1.put("card_money_max", card.getMoney_max());
		map1.put("card_money_remain", card.getMoney_remain());

		return JsonTools.toJson(map1);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_s_customer_card card, String card_date,
			String card_end_date) {
		t_s_customer_card cardDb = (t_s_customer_card) oc.findObjById(
				card.getKid(), t_s_customer_card.class);
		cardDb.setBack_type(card.getBack_type());
		cardDb.setBank(card.getBank());
		cardDb.setBank_id(card.getBank_id());
		cardDb.setBill_back_day(card.getBill_back_day());
		cardDb.setBill_day(card.getBill_day());
		cardDb.setBill_type(card.getBill_type());
		cardDb.setCard_no(card.getCard_no());
		cardDb.setCard_type(card.getCard_type());
		card.setDate(DateTools.strToDate(card_date));
		card.setEnd_date(DateTools.strToDate(card_end_date, "yyyy-MM"));
		cardDb.setIs_other_card(card.getIs_other_card());
		cardDb.setIs_past(card.getIs_past());
		cardDb.setMoney_max(card.getMoney_max());
		cardDb.setMoney_max_temp(card.getMoney_max_temp());
		cardDb.setMoney_max_super(card.getMoney_max_super());
		cardDb.setMoney_remain(card.getMoney_remain());
		cardDb.setNote(card.getNote());
		cardDb.setPast_cnt(card.getPast_cnt());
		cardDb.setPwd_pay(card.getPwd_pay());
		cardDb.setPwd_query(card.getPwd_query());
		oc.updateObj(cardDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_s_customer_card.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

}
